<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/12 0012
 * Time: 上午 1:48
 */

namespace App\Http\Classes;


use Illuminate\Support\Facades\DB;

class Sentence
{
    private $tableName = 'sentences';
    private $tableDB = null;
    private $error = 0;
    private $message = [
        0 => '无数据',
        1 => '写入数据库出错'
    ];

    public function __construct( $id = null )
    {
        $this->tableDB = DB::table( $this->tableName );

        if ( $id !== null )
        {
            $this->tableDB = $this->tableDB->where('id', '=', $id);
        }
    }

    public function addSentences( array $data = null )
    {
        $result = $this->tableDB->insert($data);
        if ($result)
        {
            return true;
        }
        else
        {
            $this->error = 1;
            return false;
        }
    }

    public function getSentencesByLessonId( $lessonId )
    {
        $sentences = $this->tableDB->where('lesson_id', '=', $lessonId)->get();

        if ( $sentences )
        {
            for ( $i = 0; $i < count($sentences); $i++ )
            {
                $sentences[$i]->sentence_audio = HOST . $sentences[$i]->sentence_audio;
                $sentences[$i]->word_audio = HOST . $sentences[$i]->word_audio;
            }
            return $sentences;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getSentenceByLessonIdWithOutRemember($lessonId, array $sentences)
    {
        $result = $this->tableDB;

        $result = $result->where('lesson_id', '=', $lessonId);

        $result = $result->whereNotIn('id', $sentences);

        $result = $result->get();

        if ($result)
        {
            for ( $i = 0; $i < count($result); $i++ )
            {
                $result[$i]->sentence_audio = HOST . $result[$i]->sentence_audio;
                $result[$i]->word_audio = HOST . $result[$i]->word_audio;
            }
            return $result;
        }
        else
        {
            $this->error = 0;
            return [];
        }
    }

    public function delSentenceById()
    {

    }

    public function getMessage()
    {
        return $this->message[ $this->error ];
    }
}